导航菜单
首页 >  Flask配置Ueditor将图片上传本地或七牛  > 百度的Ueditor上传到七牛,oss等第三方存储的通用方法(php版)

百度的Ueditor上传到七牛,oss等第三方存储的通用方法(php版)

用百度的Ueditor上传图片默认保存到本地,在分布式系统中使用非常不方便,一般需要上传到七牛,阿里云的oss上

需要七牛sdk 6.1.9(支持php5.2,高版本可自行改正)

Ueditor 1.4.3

主要原理:百度ueditor上传文件时会创建本地文件,并返回文件路径 $this->fullName,我们需要修改上传类,在上传成功时进行文件的转存,转存成功后把本地的文件删除,该方法可以处理图片,附件等小文件,大文件不建议在ueditor上上传。

分享地址:http://pan.baidu.com/s/1o8noPdg ;欢迎大家下载和共同研究

提供修改方式:(oss只要修改配置文件和上传方法就行^_^)

1.七牛官网下载sdk6.1.9,放在php的文件夹下

2.在ueditor/php下qiniu_config.php

3.修改php下的config.json,把图片访问的前缀改成相应的远程访问的前缀

"imageUrlPrefix": "", /* 图片访问路径前缀 */

4.修改主要的上传文件Uploader.class.php,将upFile方法改成如下方法

/** * 上传到七牛 * @return mixed */private function upFile(){$file = $this->file = $_FILES[$this->fileField];if (!$file) {$this->stateInfo = $this->getStateInfo("ERROR_FILE_NOT_FOUND");return;}if ($this->file['error']) {$this->stateInfo = $this->getStateInfo($file['error']);return;} else if (!file_exists($file['tmp_name'])) {$this->stateInfo = $this->getStateInfo("ERROR_TMP_FILE_NOT_FOUND");return;} else if (!is_uploaded_file($file['tmp_name'])) {$this->stateInfo = $this->getStateInfo("ERROR_TMPFILE");return;}$this->oriName = $file['name'];$this->fileSize = $file['size'];$this->fileType = $this->getFileExt();$this->fullName = $this->getFullName();$this->filePath = $this->getFilePath();$this->fileName = $this->getFileName();$dirname = dirname($this->filePath);//检查文件大小是否超出限制if (!$this->checkSize()) {$this->stateInfo = $this->getStateInfo("ERROR_SIZE_EXCEED");return;}//检查是否不允许的文件格式if (!$this->checkType()) {$this->stateInfo = $this->getStateInfo("ERROR_TYPE_NOT_ALLOWED");return;}//创建目录失败if (!file_exists($dirname) && !mkdir($dirname, 0777, true)) {$this->stateInfo = $this->getStateInfo("ERROR_CREATE_DIR");return;} else if (!is_writeable($dirname)) {$this->stateInfo = $this->getStateInfo("ERROR_DIR_NOT_WRITEABLE");return;}//移动文件if (!(move_uploaded_file($file["tmp_name"], $this->filePath) && file_exists($this->filePath))) { //移动失败$this->stateInfo = $this->getStateInfo("ERROR_FILE_MOVE");} else { //移动成功//执行到这里,代表已经完成了上传,此时系统会提示上传成功//------------------------------------------------ start ---------------------------------------------------------//上传到七牛require_once("qiniu/io.php");require_once("qiniu/rs.php");$config = require_once("qiniu_config.php");$bucket = $config['bucket']; //仓库名//截取原始文件后缀名//$key1 = "Uploads/".time().mt_rand(10,99).$this->getFileExt();$accessKey = $config['accessKey'];$secretKey = $config['secretKey'];Qiniu_SetKeys($accessKey, $secretKey);$putPolicy = new Qiniu_RS_PutPolicy($bucket);$upToken = $putPolicy->Token(null);$putExtra = new Qiniu_PutExtra();$putExtra->Crc32 = 1;//$file[ "tmp_name" ] ueditor上传的临时文件路径list($ret, $err) = Qiniu_PutFile($upToken,$this->fullName,$_SERVER['DOCUMENT_ROOT']."/".$this->fullName, $putExtra);//成功了,删除本地文件if($ret){@unlink($_SERVER['DOCUMENT_ROOT']."/".$this->fullName);//file_put_contents("./log/log.txt","\ntime:".time()." ".json_encode($ret)."\n",FILE_APPEND);}if($err){if(!is_dir("./log")){if(!mkdir("log",0755,true)){die("当前目录没有写权限");}}file_put_contents("./log/log.txt","\ntime:".time()." ".json_encode($_SERVER['DOCUMENT_ROOT']."/".$this->fullName)."\n",FILE_APPEND);$this->stateInfo = $this->getStateInfo("QINIU_ERR");}else{$this->stateInfo = $this->stateMap[0];}//------------------------------------------------- end---------------------------------------------------------}}

5.修改upBase64方法(如果不用截屏和涂鸦可以不改)

/** * 处理base64编码的图片上传 * @return mixed */private function upBase64(){$base64Data = $_POST[$this->fileField];$img = base64_decode($base64Data);$this->oriName = $this->config['oriName'];$this->fileSize = strlen($img);$this->fileType = $this->getFileExt();$this->fullName = $this->getFullName();$this->filePath = $this->getFilePath();$this->fileName = $this->getFileName();$dirname = dirname($this->filePath);//检查文件大小是否超出限制if (!$this->checkSize()) {$this->stateInfo = $this->getStateInfo("ERROR_SIZE_EXCEED");return;}//创建目录失败if (!file_exists($dirname) && !mkdir($dirname, 0777, true)) {$this->stateInfo = $this->getStateInfo("ERROR_CREATE_DIR");return;} else if (!is_writeable($dirname)) {$this->stateInfo = $this->getStateInfo("ERROR_DIR_NOT_WRITEABLE");return;}//移动文件if (!(file_put_contents($this->filePath, $img) && file_exists($this->filePath))) { //移动失败$this->stateInfo = $this->getStateInfo("ERROR_WRITE_CONTENT");} else { //移动成功require_once("qiniu/io.php");require_once("qiniu/rs.php");$config = require_once("qiniu_config.php");$bucket = $config['bucket']; //仓库名//截取原始文件后缀名//$key1 = "Uploads/".time().mt_rand(10,99).$this->getFileExt();$accessKey = $config['accessKey'];$secretKey = $config['secretKey'];Qiniu_SetKeys($accessKey, $secretKey);$putPolicy = new Qiniu_RS_PutPolicy($bucket);$upToken = $putPolicy->Token(null);$putExtra = new Qiniu_PutExtra();$putExtra->Crc32 = 1;//$file[ "tmp_name" ] ueditor上传的临时文件路径list($ret, $err) = Qiniu_PutFile($upToken,$this->fullName,$_SERVER['DOCUMENT_ROOT']."/".$this->fullName, $putExtra);if($ret){//删临时文件@unlink($_SERVER['DOCUMENT_ROOT']."/".$this->fullName); // file_put_contents("./log/log.txt","\ntime:".time()." ".json_encode($ret)."\n",FILE_APPEND);}if($err){file_put_contents("./log/log.txt","\ntime:".time()." ERR:".json_encode($err)."".json_encode($_SERVER['DOCUMENT_ROOT']."/".$this->fullName)."\n",FILE_APPEND);// $this->stateInfo = $this->getStateInfo("QINIU_ERR");}$this->stateInfo = $this->stateMap[0];}}

6.修改重命名方法,主要是随机数那里抹去两个零就可以了

/** * 重命名文件 * @return string */private function getFullName(){//替换日期事件$t = time();$d = explode('-', date("Y-y-m-d-H-i-s"));$format = $this->config["pathFormat"];$format = str_replace("{yyyy}", $d[0], $format);$format = str_replace("{yy}", $d[1], $format);$format = str_replace("{mm}", $d[2], $format);$format = str_replace("{dd}", $d[3], $format);$format = str_replace("{hh}", $d[4], $format);$format = str_replace("{ii}", $d[5], $format);$format = str_replace("{ss}", $d[6], $format);$format = str_replace("{time}", $t, $format);//过滤文件名的非法自负,并替换文件名$oriName = substr($this->oriName, 0, strrpos($this->oriName, '.'));$oriName = preg_replace("/[\|\?\"\\/\*\\\\]+/", '', $oriName);$format = str_replace("{filename}", $oriName, $format);//替换随机字符串$randNum = rand(1, 100000000) . rand(1, 100000000);if (preg_match("/\{rand\:([\d]*)\}/i", $format, $matches)) {$format = preg_replace("/\{rand\:[\d]*\}/i", substr($randNum, 0, $matches[1]), $format);}$ext = $this->getFileExt();return $format . $ext;}

 

 

转载自: https://my.oschina.net/u/2528821/blog/817896

相关推荐: